<?php
namespace App\Http\Controllers\Work;

use App\Common\Result;
use App\Http\Controllers\Controller;
use App\Models\Works\ArticleModel;
use App\Service\Customer\CustomerService;
use App\Service\Easemob\EasemobService;
use App\Service\Goods\GoodsService;
use App\Service\Jpush\JpushService;
use App\Service\Work\BestWorksService;
use App\Service\Work\CommentService;
use App\Service\Work\PraiseService;
use App\Service\Work\WorksRelateService;
use Illuminate\Http\Request;
use App\Service\ServiceLog;
use DB;
use Illuminate\Support\Facades\Redis;


class WorksController extends Controller
{
    /**
     * 推荐的商品
     * @param Request $request
     */
    public function bestWorks(Request $request)
    {

        $page = $request->input('page');
        $uid = $request->input('uid');

        $offest = ($page-1)*10;
        $works = BestWorksService::getBestWorks($uid,10,$offest);

        if($works){
            Result::msg('0',"success",$works);
        }else{
            Result::msg('-18',"error","没有更多了");
        }
    }

    /**
     * 获得附近的商品
     */
    public function nearWorks(Request $request)
    {
        $page = $request->input('page');
        $uid = $request->input('uid');
        $lon = $request->input('lon');
        $lat = $request->input('lat');
        $offest = ($page-1)*10;
        $works = BestWorksService::getNearWorks($uid,$lon,$lat,10,$offest);

        if(!$works){
            $works = BestWorksService::getNearRecommendWorks($uid,10,$offest);
        }
        Result::msg('0',"success",$works);

    }
    /**
     * 首页上半
     */
    public function firstPageHead(Request $request)
    {
        //获得banner
        $banner = WorksRelateService::getAds();

        //标签
        $label = GoodsService::getGoodsLabels();

        //新品推荐
        $news = GoodsService::newGoodsRecmd();

        $result = array(
            'banner' => $banner,
            'news' => $news,
            'label'  => $label
        );
        Result::msg('0',"success",$result);
    }

    /**
     *  获取广告
     */
    public function ads()
    {
        //获得banner
        $banner = WorksRelateService::getAds();
        Result::msg('0',"success",$banner);
    }

    /**
     *  获取所有标签类型
     */
    public function label()
    {
        //获得banner
        $label = GoodsService::getGoodsLabels();
        Result::msg('0',"success",$label);
    }

    /**
     *  首页通知
     */
    public function notice(Request $request)
    {
        $pageSize = $request->input('pageSize',5);
        $notice = WorksRelateService::getNotice($pageSize);
        Result::msg('0',"success",$notice);
    }

    /**
     * 首页商品
     * @param Request $request
     */
    public function firstPageRecmdGoods(Request $request)
    {
        $page = $request->input('page',1);
        $page_size = $request->input('pageSize',10);

        $offset = ($page-1)*$page_size;
        //查询条件
        $where = [
            'is_up' => 1
        ];

        $result = GoodsService::getGoodsList($where,2,$offset,$page_size);
        if($result->isEmpty()){
            Result::msg('700','success',[]);
        }
        Result::msg('0','success',$result);
    }

    /**
     * @param Request $request
     */
    public function goodsIndex(Request $request)
    {
        $label_id = $request->input('categoryId','');
        $page = $request->input('page',1);
        $page_size = $request->input('pageSize',10);
        $keyword = $request->input('nameLike','');
        //0综合 1销量 2新品 3价格
        $type = $request->input('type',0);
        //从高到低 还是从低到高
        $is_high = $request->input('is_high',1);

        $offset = ($page-1)*$page_size;
        //查询条件
        $where = [
            'is_up' => 1
        ];

        if($label_id){
            //存在label_id
            $where[] = ['label_id',$label_id];
        }

        if($keyword){
            $where[] = ['name','like',"%{$keyword}%"];
        }

        $result = GoodsService::getGoodsList($where,$type,$offset,$page_size,$is_high);
        if($result->isEmpty()){
            Result::msg('700','success',[]);
        }
        Result::msg('0','success',$result);
    }

    //点赞
    public function praise(Request $request)
    {
        $uid = $request->uid;
        $article_id = $request->articleid;

        $parise_info = PraiseService::getPraise([
            'uid' => $uid,
            'articleid' => $article_id
        ]);

        if($parise_info){
            Result::msg('-13','error','你已经点赞过了');
        }

        $data = [
            'uid' => $uid,
            'articleid' => $article_id,
			'created_at' => date("Y-m-d H:i:s")
        ];

        if(!PraiseService::addPraise($data)){
            Result::msg('-14','error','点赞失败');
        }

        Result::msg('0','success',$data);

    }

    //评论
    public function comment(Request $request)
    {
        Result::isEmpty($request->uid,'无有效用户信息',-16);
        Result::isEmpty($request->worksid,'无有效文章',-17);
        Result::veryShort($request->comment);

        $data = array(
            'uid' => $request->uid,
            'worksid' => $request->worksid,
            'rid' => $request->rid,
            'content' => $request->comment
        );
        //获得文章详情
        $article_info = BestWorksService::findArticle([
            'id' => $request->worksid
        ]);

        if(!$article_info){
            Result::msg('-17','error','无有效文章');
        }

		//获得评论
		if($request->rid!=0){
			
        $comment_info = CommentService::getComments($request->rid);
        if(!$comment_info){
            Result::msg('-26','error','评论失效');
        }
        if($comment_info->uid == $request->uid){
            Result::msg('-25','error','不能回复自己');
        }
		}
        //添加评论
        if(!CommentService::addComments($data)){
            Result::msg('-11','error','更新失败');
        }

        Result::msg('0','success',$data);

    }

    //获得评论列表
    public function getCommentsList(Request $request)
    {
        $article_id = $request->input('articleid');
        $page = $request->input('page');
        $offset = ($page-1)*10;
        $data = CommentService::getCommentsList($article_id, $offset);
        if(!$data){
            Result::msg('-18',"error","没有更多了");
        }
        Result::msg('0','success',$data);
    }

    //获得回复
    public function getCommentsResponce(Request $request)
    {
        $rid = $request->input('rid');
        $page = $request->input('page');
        $offset = ($page-1)*10;
        $data = CommentService::getCommentResponce($rid, $offset);
        if(!$data){
            Result::msg('-18',"error","没有更多了");
        }

        Result::msg('0',"success",$data);
    }
	
	
	/**
     * 获得单个文章
     */
    public function getArticleDetail(Request $request)
    {
        $article_id = $request->worksid;
        $uid = $request->uid;

        /*if(!CustomerService::findUser(['uid'=>$uid])){
            Result::msg('-16',"error","无有效用户信息");
        }*/

        $article_info = BestWorksService::findArticleDetail($uid,$article_id);
        if(!$article_info){
            Result::msg('-18',"error","没有更多了");
        }
        Result::msg('0',"success",$article_info);
    }
	
	 /**
     * 获得作者发布文章
     */
    public function getMyArticle(Request $request)
    {
        $page = $request->input('page');
        $uid = $request->input('uid');

        $offest = ($page-1)*10;
        //$page = $page*10;

        if(!CustomerService::findUser(['uid'=>$uid])){
            Result::msg('-16',"error","无有效用户信息");
        }

        $article_info = BestWorksService::userArticleList($uid,10,$offest);
        if(!$article_info){
            Result::msg('-18',"error","没有更多了");
        }
        Result::msg('0',"success",$article_info);
    }


    public function test()
    {
//        Redis::hmset("friends:130",
//            "author_uid", "sj_14955313882001",
//            "content", "美丽的青岛",
//            "photos", "[\"http:\/\/orixmzok6.bkt.clouddn.com\/friend\/201802272503\/sj_14955313882001\/0\/320.000000\/480.000000\/.jpg\",\"http:\/\/orixmzok6.bkt.clouddn.com\/friend\/201802272503\/sj_14955313882001\/1\/360.000000\/640.000000\/.jpg\"]",
//            "location", "杭州市",
//            "read_num", 0,
//            "comment_num", 0,
//            "share_num", 0,
//            "created","2018-02-27 15:25:05");
        $res = Redis::hgetall('friends:130');
        var_dump($res);
    }
	
	public function xcxFirstPage(Request $request)
	{
		$page = $request->input('page');
        
        $offest = ($page-1)*10;
        //$page = $page*10;
        $works = BestWorksService::xcxBestWorks(10,$offest);
        
        if($works){
            Result::msg('0',"success",$works);
        }else{
            Result::msg('-18',"error","没有更多了");
        }
	}
	
	public function xcxArticleDetail(Request $request)
    {
        $article_id = $request->input('aid');

        $articles = BestWorksService::getXcxArticleDetail($article_id);
		
		
        if(!$articles){
            Result::msg('-18',"error","文章已被删除");
        }
		
		$articles[0]->content = preg_replace("/<p.*?>/","<p>",$articles[0]->content);
		$articles[0]->content = htmlspecialchars_decode($articles[0]->content);
		$articles[0]->pay_content = htmlspecialchars_decode($articles[0]->pay_content);
		//echo $articles[0]->pay_content;
		//var_dump($articles[0]->content);
        Result::msg('0',"success",$articles);
    }
	
	/**
     * 分类展示页面
     * @param Request $request
     */
    public function OtherArticleClassWorks(Request $request)
    {

        $page = $request->input('page');
        $uid = $request->input('uid');
        $article_class = $request->input('aclass');
        $offest = ($page-1)*10;
        //$page = $page*10;
        
        /*if(!CustomerService::findUser(['uid'=>$uid])){
            Result::msg('-16',"error","无有效用户信息");
        }*/
        $works = BestWorksService::getOtherWorks($uid,$article_class,10,$offest);
        if($works){
            Result::msg('0',"success",$works);
        }else{
            Result::msg('-18',"error","没有更多了");
        }
    }
	
	/**
     * 增加阅读量
     */
    public function addReading(Request $request)
    {
        $uid = $request->uid;
        $article_id = $request->work_id;

        $my_article = BestWorksService::findArticle(['author_uid'=>$uid,'id'=>$article_id]);
        $is_article = BestWorksService::findArticle(['id'=>$article_id]);
		
        if(!$is_article){
            Result::msg('-17',"error","无有效文章");
        }

        if(!$my_article){
            //增加阅读量
            BestWorksService::addArticleReading($article_id);
            //增加阅读量缓存
            if(Redis::exists("articles:{$request->works_id}"))
                Redis::hincrby("articles:{$request->works_id}","read_num",1);
        }
        Result::msg('0',"success",[]);
    }
}
        
        